from calculator.simple import SimpleCalculator
from flask import Flask, render_template
from flask_cors import cross_origin

app = Flask(__name__)


def calcOp(text):
    """based on the input text, return the operation result"""
    try:
        c = SimpleCalculator()
        c.run(text)
        return c.log[-1]
    except Exception as e:
        print(e)
        return 0.0


@app.route('/')
def homepage():
    home = 'flask_welcome.html'
    return render_template(home)


@app.route("/<input>")
@cross_origin()
def calc(input):
    return calcOp(input)


if __name__ == "__main__":
    app.run(host='127.0.0.1', port=5001, use_reloader=False)
    # 注意，如果没有指定use_reloader=False，后续将其打包成exe后，运行exe会产生两个进程，在electron窗口关闭时kill掉进程时，会有一个守护进程无法kill掉

